// pages/bind-phone/bind-phone.js
import {
  sMSphone,
  phoneEdit,
  GetUserCRMInfo,
  categoryByLevel
} from '../../api/apis.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sendBtnMsg: 0,
    moblie: '',
    checkPhone: '',
    code: '',
    showBonus: false,
    bgImg: '',
    cardBgImg: '',
    cardList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this._initData()
  },
  _initData() {
    GetUserCRMInfo({
      data: {
        openid: app.globalData.openid
      },
      success: res => {
        console.log(res)
        if (res.code === 0) {
          this.setData({
            bindPhone: res.data.phone,
            againBind: false
          })
          this.setData({
            firstBind: this.data.bindPhone ? false : true
          })
        }
      }
    })
    this.getBg()
    this.getCardBg()
  },
  _againBind() {
    this.setData({
      againBind: true
    })
  },
  phoneInput(e) {
    this.setData({
      moblie: e.detail.value
    })
  },
  codeInput(e) {
    this.setData({
      code: e.detail.value
    })
  },
  sendVcode() {
    if (this.data.moblie.length === 11) {
      wx.showLoading({
        title: '发送中'
      })
      sMSphone({
        data: {
          openid: app.globalData.openid,
          moblie: this.data.moblie
        },
        success: res => {
          if (res.state) {
            this.setData({
              checkPhone: this.data.moblie
            })
            this.countDown()
            wx.hideLoading()
          } else {
            wx.showToast({
              title: res.message,
              icon: 'none',
              duration: 1500
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '手机号输入有误',
        icon: 'none',
        duration: 1500
      })
    }
  },
  countDown() {
    let time = 60
    let timer = null
    timer = setInterval(() => {
      if (time <= 0) {
        clearInterval(timer)
      }
      this.setData({
        sendBtnMsg: time--
      })
    }, 1000)
  },
  submit() {
    wx.showLoading({
      title: '绑定中...'
    })
    phoneEdit({
      data: {
        openid: app.globalData.openid,
        moblie: this.data.checkPhone,
        Value: this.data.code
      },
      success: res => {
        if (res.state) {
          this._initData()
          this._showBonus()
          if (res.data.length>0) {
            this.setData({
              cardList: res.data
            })
          }
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none'
          })
        }
        wx.hideLoading()
      }
    })
  },
  _showBonus() {
    if (this.data.firstBind) {
      this.setData({
        showBonus: true
      })
    }
  },
  _hideBonus() {
    this.setData({
      showBonus: false
    })
  },
  getBg() {
    categoryByLevel({
      data: {
        menuID: 5,
        id: 179,
        level: 2
      },
      success: res => {
        if (res.state) {
          this.setData({
            bgImg: res.data[0].Img
          })
        }
      }
    })
  },
  getCardBg() {
    categoryByLevel({
      data: {
        menuID: 5,
        id: 179,
        level: 2
      },
      success: res => {
        if (res.state) {
          this.setData({
            cardBgImg: res.data[1].Img
          })
        }
      }
    })
  },
  _addCard() {
    wx.addCard({
      cardList: this.data.cardList,
      success: res => {
        wx.showToast({
          title: '成功领取卡券至微信卡包',
          icon: 'none',
          duration: 2000
        })
      },
      fail: res => {
        wx.showToast({
          title: '领取卡券失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  }
})